﻿using System;
using System.Drawing;
using System.Windows.Forms;
/* W tym miejscu dołączamy biblioteki */
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;
using Microsoft.DirectX.DirectInput;
using MyDX;
using D3D = Microsoft.DirectX.Direct3D;



namespace scena2
{
    public class WinForm : System.Windows.Forms.Form     
    {

        private D3D.Device device;  //tu pierwsza nowość. Zmienna typu Device.

        private System.ComponentModel.Container components = null;
        private Microsoft.DirectX.DirectInput.Device keyboard;
        private Microsoft.DirectX.DirectInput.Device mouse;

        private MyCamera Camera, Light, RefCamSphere, RefCamSat2;
        private Vector3 camdir = new Vector3(0, 0, 1);

        float angle1 = 0.0f, angle2 = 0.0f;

        float spd = 0.1f, rotspd = 0.005f;

        float time = 0.0f;

        float dt2 = 1.0f;
        static long old;

        private int MapSize = 512;
        private int MapSize2 = 512;
        private D3D.Font Font3D;

        private CubeTexture Kostka;
        private CubeTexture r_m1, r_o1, r_m2, r_o2;
        private CubeTexture draw1, draw2, old1, old2;

        private Matrix mProjectionCube, mViewCube;

        private float fps;
        private int frames = 0, PrevTickCount = 1;
        private D3D.Effect effect = null;

        private Matrix mProjection, mView;
        private float shin = 300.0f;
        private Vector4 LightPosition;
        private Vector4 CameraPosition;

        private MouseState state1;
        private MyObject box;
        private MyObject satellite2;
        private MyObject sphere;
        private MyObject woman;
        private MyObject satellite1;

        Surface RenderSurface;
        private RenderToSurface Rts, Rts2, Rts3;

        private int smapsize = 512;
        private Texture smap;
        float dd2 = 0.009f;
        private Texture smap2;

        private Texture gauss;

        public WinForm()  // konstruktor - inicjalizujemy komponenty formy (tak naprawdę to ich nie ma, ale wygodniej się pracuje w VS)
        {
            InitializeComponent();

        }


        public void InitializeKeyboard()
        {
            keyboard = new Microsoft.DirectX.DirectInput.Device(SystemGuid.Keyboard);
            keyboard.SetCooperativeLevel(this, CooperativeLevelFlags.Background | CooperativeLevelFlags.NonExclusive);
            keyboard.Acquire();

            mouse = new Microsoft.DirectX.DirectInput.Device(SystemGuid.Mouse);
            mouse.Properties.AxisModeAbsolute = true;
            mouse.SetCooperativeLevel(this, CooperativeLevelFlags.Background | CooperativeLevelFlags.NonExclusive);

            mouse.Acquire();


            state1 = mouse.CurrentMouseState;
        }

        private void UpdateFramerate()
        {
            frames++;
            if (Math.Abs(Environment.TickCount - PrevTickCount) > 1000)
            {
                fps = (float)frames * 1000 / Math.Abs(Environment.TickCount - PrevTickCount);
                PrevTickCount = Environment.TickCount;
                frames = 0;
            }

        }

        public void InitializeMeshes()
        {

            Camera = new MyCamera();
            Light = new MyCamera();
            RefCamSphere = new MyCamera();
            RefCamSat2 = new MyCamera();
            Camera.Position = new Vector3(0, 2, -8.5f);

            box = new MyObject(ref device);
            box.CreateQuadrangle(20, 20);
            box.LoadTexture(0, "C:\\x\\mars.bmp");

            satellite2 = new MyObject(ref device);
            satellite2.LoadFromFile("C:\\x\\sphere.X");
            satellite2.Normalize(Matrix.Scaling(0.5f, 0.5f, 0.5f));
            satellite2.NormalizeVB();

            sphere = new MyObject(ref device);
            sphere.LoadFromFile("C:\\x\\sphere.X");
            sphere.Normalize(Matrix.Scaling(1.5f, 1.5f, 1.5f));
            sphere.NormalizeVB();


            satellite1 = new MyObject(ref device);
            satellite1.LoadFromFile("C:\\x\\kulka2.X");
            satellite1.LoadTexture(0, "c:\\x\\grass.JPG");
            satellite1.Normalize(Matrix.Identity);
            satellite1.NormalizeVB();

            woman = new MyObject(ref device);
            woman.LoadFromFile("C:\\x\\woman.X");
            woman.LoadTexture(0, "c:\\x\\rockbump.JPG");
            woman.Normalize(Matrix.Identity);
            woman.NormalizeVB();

            sphere.Position.Y = 2.5f;
            woman.Position = new Vector3(5, 2, 15);
            woman.Scale = 3.0f;
            satellite2.Position = new Vector3(0, 1.5f, 8.0f);
            satellite1.Position = new Vector3(-5, 2, -5);

            Kostka = new CubeTexture(device, MapSize, 1, Usage.RenderTarget, Format.R32F, Pool.Default);

            r_m1 = new CubeTexture(device, MapSize2, 1, Usage.RenderTarget, Format.X8R8G8B8, Pool.Default);
            r_o1 = new CubeTexture(device, MapSize2, 1, Usage.RenderTarget, Format.X8R8G8B8, Pool.Default);

            r_m2 = new CubeTexture(device, MapSize2, 1, Usage.RenderTarget, Format.X8R8G8B8, Pool.Default);
            r_o2 = new CubeTexture(device, MapSize2, 1, Usage.RenderTarget, Format.X8R8G8B8, Pool.Default);

            smap = new Texture(device, smapsize, smapsize, 1, Usage.RenderTarget, Format.X8R8G8B8, Pool.Default);
            smap2 = new Texture(device, smapsize, smapsize, 1, Usage.RenderTarget, Format.X8R8G8B8, Pool.Default);

            gauss = TextureLoader.FromFile(device, "..\\..\\gauss.png");

            draw1 = r_m1;
            draw2 = r_m2;
            old1 = r_o1;
            old2 = r_o2;
        }

        public void RenderLight(Matrix View, Matrix Projection)
        {
            Vector3 tmp2 = new Vector3(LightPosition.X, LightPosition.Y, LightPosition.Z);

            Matrix tmp = Matrix.Scaling(0.1f, 0.1f, 0.1f);

            tmp.Multiply(Matrix.Translation(tmp2));

            effect.SetValue("xRotationMatrix", Matrix.Identity);
            device.SetStreamSource(0, sphere.vb, 0);
            device.VertexDeclaration = sphere.vd;
            effect.SetValue("xWorld", tmp);
            effect.SetValue("xView", View);
            effect.SetValue("xProjection", Projection);

            int passes = effect.Begin(0);
            for (int i = 0; i < passes; i++)
            {
                effect.BeginPass(i);
                sphere.Render(ref effect, "xTexture");
                effect.EndPass();
            }
        }

        public void RenderObject(MyBaseObject O, Matrix View, Matrix Projection)
        {
            effect.SetValue("xRotationMatrix", Matrix.RotationQuaternion(O.Rotation));
            device.SetStreamSource(0, O.vb, 0);
            device.VertexDeclaration = O.vd;
            effect.SetValue("xWorld", O.getmat());
            effect.SetValue("xView", View);
            effect.SetValue("xProjection", Projection);
            int passes = effect.Begin(0);
            for (int i = 0; i < passes; i++)
            {
                effect.BeginPass(i);
                O.Render(ref effect, "xTexture");
                effect.EndPass();
            }
            effect.End();
        }

        public void UpdateCamera()
        {
            float aspect_ratio = this.Width / (float)this.Height;
            mProjection = Matrix.PerspectiveFovLH((float)Math.PI / 4, aspect_ratio, 0.02f, box.radius * 5);  // macierz projekcji
            mView = Camera.ViewMatrix(Camera.Position + camdir);
            CameraPosition = new Vector4(Camera.Position.X, Camera.Position.Y, Camera.Position.Z, 1.0f);
        }

        public void InitializeDevice() // Druga nowość. Inicjujemy urządzenie.
        {
            PresentParameters presentParams = new PresentParameters();
            presentParams.BackBufferWidth = 1024;
            presentParams.BackBufferHeight = 768;
            presentParams.BackBufferFormat = Format.A8R8G8B8;
            presentParams.BackBufferCount = 1;

            presentParams.Windowed = true;
            presentParams.SwapEffect = SwapEffect.Discard;
            presentParams.AutoDepthStencilFormat = DepthFormat.D16;
            presentParams.EnableAutoDepthStencil = true;
            device = new D3D.Device(0, D3D.DeviceType.Hardware, this, CreateFlags.HardwareVertexProcessing, presentParams);
            System.Drawing.Font Font2D = new System.Drawing.Font("Arial", 12f, FontStyle.Regular);
            Font3D = new D3D.Font(device, Font2D);



            string err;
            effect = D3D.Effect.FromFile(device, "..\\..\\a.fx", null, ShaderFlags.None, null, out err);
            if (effect == null)
            {
                MessageBox.Show(err);
            }

            Rts = new RenderToSurface(device, MapSize, MapSize, Format.R32F, true, DepthFormat.D16);

            Rts2 = new RenderToSurface(device, MapSize2, MapSize2, Format.X8R8G8B8, true, DepthFormat.D16);

            Rts3 = new RenderToSurface(device, smapsize, smapsize, Format.X8R8G8B8, true, DepthFormat.D16);
        }

        private void ReadKeyboard()
        {

            KeyboardState keys = keyboard.GetCurrentKeyboardState();

            MouseState state2 = mouse.CurrentMouseState;

            int dx = state2.X - state1.X;
            int dy = state2.Y - state1.Y;

            state1 = state2;

            angle1 += dx * rotspd;
            angle2 += dy * rotspd;

            if (angle2 < -(float)(Math.PI) / 2.0f) angle2 = -(float)(Math.PI) / 2.0f;
            if (angle2 > (float)(Math.PI) / 2.0f) angle2 = (float)(Math.PI) / 2.0f;

            camdir = new Vector3(0, 0, 1);
            Vector3 camdir2 = new Vector3(1, 0, 0);
            Vector3 camdir3 = new Vector3(0, 1, 0);
            Quaternion q = Quaternion.RotationYawPitchRoll(angle1, angle2, 0);
            Matrix mm = Matrix.RotationQuaternion(q);
            camdir.TransformNormal(mm);
            camdir2.TransformNormal(mm);
            camdir3.TransformNormal(mm);
            Camera.Up = camdir3;

            if (keys[Key.Left])
            {
                Camera.Position -= (camdir2 * spd * dt2);
            }
            if (keys[Key.Right])
            {
                Camera.Position += (camdir2 * spd * dt2);
            }

            if (keys[Key.Up])
            {
                Camera.Position += (camdir * spd * dt2);
            }
            if (keys[Key.Down])
            {
                Camera.Position -= (camdir * spd * dt2);
            }

            if (keys[Key.Escape])
            {
                Application.Exit();
            }
            if (keys[Key.N])
            {
                dd2 += 0.00001f;
            }
            if (keys[Key.M])
            {
                dd2 -= 0.00001f;
                if (dd2 <= 0.00001f) dd2 = 0.00001f;
            }
        }

        protected override void Dispose(bool disposing) // tu zwalniamy wszystkie komponenty
        {
            if (disposing)
            {
                if (components != null)
                {
                    components.Dispose();
                }
            }
            base.Dispose(disposing);
        }
        private void InitializeComponent() // tu metoda inicjująca komponenty
        {
            this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.Opaque, true); //tu drugi dodatek, aby Windows dobrze przerysowywał okno.

            this.ClientSize = new System.Drawing.Size(1024, 768);
            this.Name = "WinForm";
            this.Text = "Ostatnie starcie";
        }

        static void Main()  // tu uruchamiamy aplikację.
        {
            using (WinForm dx_form = new WinForm())
            {
                dx_form.InitializeKeyboard();
                dx_form.InitializeDevice(); // Czwarta nowość. Trzeba zainicjować Device
                dx_form.InitializeMeshes();
                old = System.Diagnostics.Stopwatch.GetTimestamp();
                Application.Run(dx_form);
            }
        }

        public void Shadow1()
        {
            effect.Technique = "flatshadow";
            effect.SetValue("xLightPosition", new Vector4(Light.Position.X, Light.Position.Y, Light.Position.Z, 1.0f));

            RenderSurface = smap.GetSurfaceLevel(0);
            Rts3.BeginScene(RenderSurface);
            device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.Black, 1.0f, 0);
            RenderAll(mView, mProjection);
            Rts3.EndScene(Filter.None);

            smap.GenerateMipSubLevels();
        }

        public void Shadow2test()
        {
            effect.Technique = "shadowtest";
            effect.SetValue("xLightPosition", new Vector4(Light.Position.X, Light.Position.Y, Light.Position.Z, 1.0f));

            RenderSurface = smap2.GetSurfaceLevel(0);
            Rts3.BeginScene(RenderSurface);
            device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.Black, 1.0f, 0);
            RenderAll(mView, mProjection);
            Rts3.EndScene(Filter.None);

            smap2.GenerateMipSubLevels();
        }

        public void DepthRender()
        {

            device.RenderState.CullMode = Cull.None;

            effect.Technique = "cubedepth"; //!!!! Technika na depth

            effect.SetValue("xLightPosition", new Vector4(Light.Position.X, Light.Position.Y, Light.Position.Z, 1.0f));
            mProjectionCube = Matrix.PerspectiveFovLH((float)Math.PI / 2.0f, 1.0f, 0.02f, box.radius * 5);  // macierz projekcji swiatla

            RenderSurface = Kostka.GetCubeMapSurface(CubeMapFace.NegativeX, 0);
            mViewCube = Matrix.LookAtLH(Light.Position, Light.Position + new Vector3(-1, 0, 0), new Vector3(0, 1, 0));
            Rts.BeginScene(RenderSurface);
            device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.Black, 1.0f, 0);
            RenderAll(mViewCube, mProjectionCube);
            Rts.EndScene(Filter.None);

            mViewCube = Matrix.LookAtLH(Light.Position, Light.Position + new Vector3(1, 0, 0), new Vector3(0, 1, 0));
            RenderSurface = Kostka.GetCubeMapSurface(CubeMapFace.PositiveX, 0);
            Rts.BeginScene(RenderSurface);
            device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.Black, 1.0f, 0);
            RenderAll(mViewCube, mProjectionCube);
            Rts.EndScene(Filter.None);

            mViewCube = Matrix.LookAtLH(Light.Position, Light.Position + new Vector3(0, 1, 0), new Vector3(0, 0, -1));
            RenderSurface = Kostka.GetCubeMapSurface(CubeMapFace.PositiveY, 0);
            Rts.BeginScene(RenderSurface);
            device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.Black, 1.0f, 0);
            RenderAll(mViewCube, mProjectionCube);
            Rts.EndScene(Filter.None);

            mViewCube = Matrix.LookAtLH(Light.Position, Light.Position + new Vector3(0, -1, 0), new Vector3(0, 0, 1));
            RenderSurface = Kostka.GetCubeMapSurface(CubeMapFace.NegativeY, 0);
            Rts.BeginScene(RenderSurface);
            device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.Black, 1.0f, 0);
            RenderAll(mViewCube, mProjectionCube);
            Rts.EndScene(Filter.None);

            mViewCube = Matrix.LookAtLH(Light.Position, Light.Position + new Vector3(0, 0, 1), new Vector3(0, 1, 0));
            RenderSurface = Kostka.GetCubeMapSurface(CubeMapFace.PositiveZ, 0);
            Rts.BeginScene(RenderSurface);
            device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.Black, 1.0f, 0);
            RenderAll(mViewCube, mProjectionCube);
            Rts.EndScene(Filter.None);

            mViewCube = Matrix.LookAtLH(Light.Position, Light.Position + new Vector3(0, 0, -1), new Vector3(0, 1, 0));
            RenderSurface = Kostka.GetCubeMapSurface(CubeMapFace.NegativeZ, 0);
            Rts.BeginScene(RenderSurface);
            device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.Black, 1.0f, 0);
            RenderAll(mViewCube, mProjectionCube);
            Rts.EndScene(Filter.None);

            device.RenderState.CullMode = Cull.CounterClockwise;
        }

        public void ReflRender1(CubeTexture tx, CubeTexture old, MyBaseObject flat, MyBaseObject skip, MyCamera cam)
        {

            effect.Technique = "lighted2";
            effect.SetValue("ref_old", old);
            effect.SetValue("xReflectionCube", old);
            effect.SetValue("xLightPosition", new Vector4(Light.Position.X, Light.Position.Y, Light.Position.Z, 1.0f));
            mProjectionCube = Matrix.PerspectiveFovLH((float)Math.PI / 2.0f, 1.0f, 0.02f, box.radius * 5);  // macierz projekcji swiatla

            RenderSurface = tx.GetCubeMapSurface(CubeMapFace.NegativeX, 0);
            mViewCube = Matrix.LookAtLH(cam.Position, cam.Position + new Vector3(-1, 0, 0), new Vector3(0, 1, 0));
            Rts2.BeginScene(RenderSurface);
            device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.Black, 1.0f, 0);
            RenderAll2(mViewCube, mProjectionCube, flat, skip);
            Rts2.EndScene(Filter.None);

            mViewCube = Matrix.LookAtLH(cam.Position, cam.Position + new Vector3(1, 0, 0), new Vector3(0, 1, 0));
            RenderSurface = tx.GetCubeMapSurface(CubeMapFace.PositiveX, 0);
            Rts2.BeginScene(RenderSurface);
            device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.Black, 1.0f, 0);
            RenderAll2(mViewCube, mProjectionCube, flat, skip);
            Rts2.EndScene(Filter.None);

            mViewCube = Matrix.LookAtLH(cam.Position, cam.Position + new Vector3(0, 1, 0), new Vector3(0, 0, -1));
            RenderSurface = tx.GetCubeMapSurface(CubeMapFace.PositiveY, 0);
            Rts2.BeginScene(RenderSurface);
            device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.Black, 1.0f, 0);
            RenderAll2(mViewCube, mProjectionCube, flat, skip);
            Rts2.EndScene(Filter.None);

            mViewCube = Matrix.LookAtLH(cam.Position, cam.Position + new Vector3(0, -1, 0), new Vector3(0, 0, 1));
            RenderSurface = tx.GetCubeMapSurface(CubeMapFace.NegativeY, 0);
            Rts2.BeginScene(RenderSurface);
            device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.Black, 1.0f, 0);
            RenderAll2(mViewCube, mProjectionCube, flat, skip);
            Rts2.EndScene(Filter.None);

            mViewCube = Matrix.LookAtLH(cam.Position, cam.Position + new Vector3(0, 0, 1), new Vector3(0, 1, 0));
            RenderSurface = tx.GetCubeMapSurface(CubeMapFace.PositiveZ, 0);
            Rts2.BeginScene(RenderSurface);
            device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.Black, 1.0f, 0);
            RenderAll2(mViewCube, mProjectionCube, flat, skip);
            Rts2.EndScene(Filter.None);

            mViewCube = Matrix.LookAtLH(cam.Position, cam.Position + new Vector3(0, 0, -1), new Vector3(0, 1, 0));
            RenderSurface = tx.GetCubeMapSurface(CubeMapFace.NegativeZ, 0);
            Rts2.BeginScene(RenderSurface);
            device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.Black, 1.0f, 0);
            RenderAll2(mViewCube, mProjectionCube, flat, skip);
            Rts2.EndScene(Filter.None);
        }


        protected void RenderAll(Matrix View, Matrix Projection)
        {
            RenderObject(box, View, Projection);
            RenderObject(sphere, View, Projection);
            RenderObject(woman, View, Projection);
            RenderObject(satellite1, View, Projection);
            RenderObject(satellite2, View, Projection);
        }

        protected void RenderAll1(Matrix View, Matrix Projection)
        {
            RenderObject(box, View, Projection);
            RenderObject(woman, View, Projection);
            RenderObject(satellite1, View, Projection);
            RenderObject(satellite2, View, Projection);
        }

        protected void RenderAll2(Matrix View, Matrix Projection, MyBaseObject flat, MyBaseObject skip)
        {
            effect.Technique = "lighted2";
            if (flat == sphere)
            {
                RenderObject(box, View, Projection);
                effect.Technique = "lighted2ref";
                RenderObject(sphere, View, Projection);
                effect.Technique = "lighted2";
                RenderObject(woman, View, Projection);
                RenderObject(satellite1, View, Projection);
            }
            else
            {
                RenderObject(box, View, Projection);
                RenderObject(woman, View, Projection);
                RenderObject(satellite1, View, Projection);
                effect.Technique = "lighted2ref";
                RenderObject(satellite2, View, Projection);
            }
        }

        protected override void OnPaint(System.Windows.Forms.PaintEventArgs e) //Trzecia nowość. W zdarzeniu OnPaint będziemy rysować. 
        {

            ReadKeyboard();
            UpdateCamera();
            UpdateFramerate();

            RefCamSphere.Position = sphere.Position;
            RefCamSat2.Position = satellite2.Position;

                time += 0.05f * dt2;

                satellite1.RotateY(0.01f * dt2);
                woman.RotateZ(0.01f * dt2);

                Vector3 a1 = new Vector3(3, 2, 4);
                Vector3 a2 = new Vector3(4, 3, 2);
                Vector3 a3 = new Vector3(3, 5, 1);
                Vector3 a4 = new Vector3(1, 3, 2);
                Vector3 a5 = new Vector3(2, 1, 4);
                a1.TransformNormal(Matrix.RotationYawPitchRoll(time / 4, time / 3, 0));
                a2.TransformNormal(Matrix.RotationYawPitchRoll(time / 6, time / 2, 0));
                a3.TransformNormal(Matrix.RotationYawPitchRoll(time / 3, time / 8, 0));
                a4.TransformNormal(Matrix.RotationYawPitchRoll(time / 9, time / 2, 0));
                a5.TransformNormal(Matrix.RotationYawPitchRoll(time / 2, time / 5, time / 9));
                woman.Position = a1 + new Vector3(0, 7, 0);
                satellite1.Position = a3 + new Vector3(0, 4, 0);
                satellite2.Position = a5 + new Vector3(0, 5, 0);

                a4 += new Vector3(0, 9, 0);
                LightPosition = new Vector4(a4.X, a4.Y, a4.Z, 1.0f);
                Light.Position = a4;

            if (effect != null)
            {
                device.RenderState.ColorWriteEnable = ColorWriteEnable.Red;
                DepthRender();
                device.RenderState.ColorWriteEnable = ColorWriteEnable.RedGreenBlueAlpha;

                Shadow1();
                Shadow2test();
                ReflRender1(draw1, old2, satellite2, sphere, RefCamSphere);
                ReflRender1(draw2, old1, sphere, satellite2, RefCamSat2);
            }


            device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.Black, 1.0f, 0);
            device.BeginScene(); // początek sceny 


            if (effect != null)
            {
                effect.SetValue("xCubeShadowMap", Kostka);
                effect.SetValue("smap", smap2);
                effect.SetValue("t_gauss", gauss);
                effect.SetValue("xWorld", Matrix.Identity);
                effect.SetValue("xView", mView);
                effect.SetValue("xProjection", mProjection);
                effect.SetValue("xLightPosition", LightPosition);
                effect.SetValue("xCameraPosition", CameraPosition);
                effect.SetValue("shininess", shin);
                effect.SetValue("d_2", dd2);
                effect.SetValue("c_ambient", new Vector4(0.1f, 0.1f, 0.1f, 1.0f));
                effect.SetValue("c_diffuse", new Vector4(1.0f, 1.0f, 1.0f, 1.0f));
                effect.SetValue("c_specular", new Vector4(1.0f, 1.0f, 1.0f, 1.0f));

                effect.CommitChanges();

                effect.Technique = "lighted2final";
                RenderObject(box, mView, mProjection);
                RenderObject(woman, mView, mProjection);
                RenderObject(satellite1, mView, mProjection);

                effect.Technique = "lighted2ref";
                effect.SetValue("xReflectionCube", draw1);
                RenderObject(sphere, mView, mProjection);

                effect.Technique = "lighted2ref";
                effect.SetValue("xReflectionCube", draw2);
                RenderObject(satellite2, mView, mProjection);


                effect.Technique = "flat";
                RenderLight(mView, mProjection);

                effect.End();

            }

            Font3D.DrawText(null, string.Format("{0:0.00} FPS", fps), new Point(10, 10), Color.White);
          //  Font3D.DrawText(null, string.Format("d: {0:0.00000}", dd2), new Point(10, 90), Color.White);

            device.EndScene();  //koniec sceny

            device.Present(); //tu informujemy, że Device już jest gotowe.

            this.Invalidate(); //to po to aby windows przerysował formę

            CubeTexture tmp = draw1;
            draw1 = old1;
            old1 = tmp;

            tmp = draw2;
            draw2 = old2;
            old2 = tmp;
        }
    }
}
